package com.jkoss.wine.shop.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.customer.entity.Shop;
import com.jkoss.wine.investment.entity.Investment;
import com.jkoss.wine.shop.service.INewShopService;
import com.jkoss.wine.system.entity.MerchantEmployees;

/**
 * 店铺 前端控制器
 * 
 * @Author momo
 * @Version 1.0, 2019-08-13
 * @See
 * @Since com.jkoss.wine.shop.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/shop/shop")
public class NewShopController extends BaseController {

	@Autowired
	private INewShopService iShopService;

	@RequestMapping("/list")
	@RequiresPermissions("/shop/shop/list")
	public String list(DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {
		Wrapper wrapper = Condition.create().eq("s.del", 0);
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			// 默认排序字段
			wrapper.orderBy("shopfwtm", false);
		}
		Page resultPage = iShopService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "shop/shop/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/shop/shop/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "shop/shop/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/shop/shop/toInsert")
	@ResponseBody
	public Object insert(@Valid Shop shop, HttpServletRequest request, HttpServletResponse response) {
		// 获取数据库里的店铺号最大的一个，递增+1
		List selectOrderByDesc = iShopService.selectOrderByDesc();
		Shop shopOrder = (Shop) selectOrderByDesc.get(0);
		Integer LargeShopNum = shopOrder.getShopNum();
		shop.setShopNum(LargeShopNum + 1);
		// 判断手机号码是否已存在
		Wrapper wrapper = Condition.create().eq("mobile_phone", shop.getMobilePhone());
		Shop shop2 = iShopService.selectOne(wrapper);
		if (!CommonUtil.isBlank(shop2)) {
			return ajaxError("电话已存在");
		}

		// 设置当前用户id为操作人
		shop.setOperator(getUserId());
		shop.setBelongs(getUserId());

		if (iShopService.insert(shop)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/shop/shop/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iShopService.selectById(id));
		return "shop/shop/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/shop/shop/toUpdate")
	@ResponseBody
	public Object update(@Valid Shop shop, HttpServletRequest request, HttpServletResponse response) {
		if (iShopService.updateById(shop)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/shop/shop/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList();
		for (String string : id) {
			Shop shop = iShopService.selectById(string);
			shop.setShopId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			shop.setDel(1);
			list.add(shop);
		}
		if (iShopService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

}
